package pl.edu.agh.administracja.networkconfig.view;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import pl.edu.agh.administracja.networkconfig.model.WirelessInterfaceConfig;
import pl.edu.agh.administracja.networkconfig.model.WirelessNetwork;
import pl.edu.agh.administracja.networkconfig.view.model.WirelessNetworkTableModel;

/**
 * Dialog przy pomocy ktorego mozna wybrac siec bezprzewodowa.
 * @author piotrek
 */
public class SelectWirelessNetworkDialog extends javax.swing.JDialog {

    private WirelessInterfaceConfig wirelessInterfaceConfig;
    private WirelessNetwork selectedWirelessNetwork = null;
    private WirelessNetworkTableModel wirelessNetworkTableModel;
    private JTable table;
    
    public static WirelessNetwork getWirelessNetwork(java.awt.Frame parent, WirelessInterfaceConfig wirelessInterfaceConfig){
        
        SelectWirelessNetworkDialog dialog = new SelectWirelessNetworkDialog(parent, true, wirelessInterfaceConfig);
        dialog.setVisible(true);
        
        return dialog.selectedWirelessNetwork;
    }
    
    private SelectWirelessNetworkDialog(java.awt.Frame parent, boolean modal, WirelessInterfaceConfig wirelessInterfaceConfig) {
        super(parent, modal);
        
        this.wirelessInterfaceConfig = wirelessInterfaceConfig;
        
        initComponents();
        updateView();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        titlePanel = new javax.swing.JPanel();
        titleLabel = new javax.swing.JLabel();
        tablePanel = new javax.swing.JScrollPane();
        buttonsPanel = new javax.swing.JPanel();
        refreshButton = new javax.swing.JButton();
        connectButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setPreferredSize(new java.awt.Dimension(700, 600));

        titleLabel.setFont(new java.awt.Font("DejaVu Sans", 1, 14)); // NOI18N
        titleLabel.setText("Wybierz sieć bezprzewodową");
        titlePanel.add(titleLabel);

        getContentPane().add(titlePanel, java.awt.BorderLayout.PAGE_START);
        getContentPane().add(tablePanel, java.awt.BorderLayout.CENTER);

        buttonsPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        refreshButton.setText("Odśwież");
        refreshButton.setPreferredSize(new java.awt.Dimension(80, 30));
        refreshButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                refreshButtonActionPerformed(evt);
            }
        });
        buttonsPanel.add(refreshButton);

        connectButton.setText("Połącz");
        connectButton.setPreferredSize(new java.awt.Dimension(80, 30));
        connectButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                connectButtonActionPerformed(evt);
            }
        });
        buttonsPanel.add(connectButton);

        cancelButton.setText("Anuluj");
        cancelButton.setPreferredSize(new java.awt.Dimension(80, 30));
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });
        buttonsPanel.add(cancelButton);

        getContentPane().add(buttonsPanel, java.awt.BorderLayout.PAGE_END);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_refreshButtonActionPerformed
        updateView();
    }//GEN-LAST:event_refreshButtonActionPerformed

    private void connectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_connectButtonActionPerformed
        if(table == null)
            return;
        
        int row = table.getSelectedRow();
        if(row == -1)
            return;
        
        selectedWirelessNetwork = wirelessNetworkTableModel.getWirelessNetworkAt(row);
        setVisible(false);
    }//GEN-LAST:event_connectButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        setVisible(false);
    }//GEN-LAST:event_cancelButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel buttonsPanel;
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton connectButton;
    private javax.swing.JButton refreshButton;
    private javax.swing.JScrollPane tablePanel;
    private javax.swing.JLabel titleLabel;
    private javax.swing.JPanel titlePanel;
    // End of variables declaration//GEN-END:variables

    private void updateView() {
        
        JPanel pleaseWaitPanel = new JPanel();
        pleaseWaitPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        JProgressBar progressBar = new JProgressBar();
        progressBar.setIndeterminate(true);
        progressBar.setPreferredSize(new Dimension(300,30));
        pleaseWaitPanel.add(progressBar);
        
        tablePanel.setViewportView(pleaseWaitPanel);
        
        new WaitForTableThread().start();
        
    }
    
    private class WaitForTableThread extends Thread{

        @Override
        public void run() {
            wirelessNetworkTableModel = new WirelessNetworkTableModel(wirelessInterfaceConfig);
            synchronized(wirelessNetworkTableModel){
                wirelessNetworkTableModel.init();
                while(!wirelessNetworkTableModel.isInitialized())
                    try {
                    wirelessNetworkTableModel.wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(SelectWirelessNetworkDialog.class.getName()).log(Level.SEVERE, null, ex);
                    return;
                }
            }
            
            SwingUtilities.invokeLater(new SetTableTask());
        }   
    }
    
    private class SetTableTask implements Runnable{

        @Override
        public void run() {
            table = new JTable(wirelessNetworkTableModel);
            tablePanel.setViewportView(table);
        }
        
    }
}
